package cn.itcast.tanhua.mytanhuaserver.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.itcast.tanhua.mytanhuaserver.mapper.BlackListMapper;
import cn.itcast.tanhua.mytanhuaserver.service.BlacklistService;
import cn.itcast.tanhua.pojo.BlackList;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 黑名单列表
 * @author 陈英航
 * @since 2021/12/6 11:35
 */
@Service
public class BlacklistServiceImpl implements BlacklistService {

    @Autowired
    private BlackListMapper blacklistMapper;

    /**
     * 查询黑名单列表
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public IPage<BlackList> queryBlacklist(Long userId, Integer page, Integer pageSize) {
        //获取到当前用户id
        QueryWrapper<BlackList> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.orderByDesc("created");
        Page<BlackList> pager = new Page<>(page, pageSize);
        return this.blacklistMapper.selectPage(pager, wrapper);

    }

    /**
     * 移除黑名单
     * @param userId
     * @param blackUserId
     * @return
     */
    @Override
    public Boolean delBlacklist(Long userId, Long blackUserId) {
        QueryWrapper<BlackList> wrapper = new QueryWrapper<BlackList>();
        wrapper.eq("user_id", userId).eq("black_user_id", blackUserId);
        return this.blacklistMapper.delete(wrapper) > 0;
    }


    /**
     * 添加黑名单
     * @param userId
     * @param blackUserId
     * @return
     */
    @Override
    public Boolean addBlacklist(Long userId,Long blackUserId){

        //判断用户是否已经是黑名单，是就返回false
        if(this.queryBlacklist(userId, blackUserId)){
            return false;
        }

        BlackList blackList = new BlackList();
        blackList.setId(Convert.toLong(ObjectId.get()));
        blackList.setUserId(userId);
        blackList.setBlackUserId(blackUserId);
        blackList.setCreated(new Date(System.currentTimeMillis()));
        blackList.setUpdated(new Date(System.currentTimeMillis()));

        if (ObjectUtil.isNotEmpty(blackList)){
            return this.blacklistMapper.insert(blackList) > 0;
        }

        return false;
    }

    /**
     * 根据用户id获取到每一个黑名单id
     * @param userId
     * @return
     */
    @Override
    public List<Long> queryBlacklistById(Long userId){
        QueryWrapper<BlackList> wrapper = new QueryWrapper<BlackList>();
        wrapper.eq("user_id", userId).select("black_user_id");
        List<BlackList> blackLists = this.blacklistMapper.selectList(wrapper);

        List<Long> blackUserId = new ArrayList<>();

        for (BlackList blackList : blackLists) {
            blackUserId.add(blackList.getBlackUserId());
        }
        return blackUserId;

    }


    @Override
    public Boolean queryIfBlacklist(Long blackUserId,Long userId) {
        //获取到当前用户id
        QueryWrapper<BlackList> wrapper = new QueryWrapper<>();
        wrapper.eq("black_user_id",blackUserId).eq("user_id", userId);
        BlackList blackList = this.blacklistMapper.selectOne(wrapper);

        if (ObjectUtil.isNotEmpty(blackList)){
            return true;
        }
        return false;
    }

    /**
     * 查詢是否已經在黑名單上
     * @param userId
     * @return
     */
    public Boolean queryBlacklist(Long userId, Long blackUserId){

        //获取到当前用户id
        QueryWrapper<BlackList> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("black_user_id",blackUserId);
        BlackList blackList = this.blacklistMapper.selectOne(wrapper);


        if (ObjectUtil.isNotEmpty(blackList)){
            return true;
        }
        return false;
    }



}
